// src/router/index.js

import {createRouter, createWebHashHistory, createWebHistory} from 'vue-router'
import routes from "@/pages/index.js";
import adminRoutes from '@/admin/index.js'
import settings from "../settings.js";

const router = createRouter({
    // import.meta.env.BASE_URL 指向 vite.config.js 中的 base 配置
    history: settings.routeMode === 'history'
        ? createWebHistory(import.meta.env.BASE_URL)
        : createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/about',
            name: 'about',
            component: () => import('@/pages/about/page.vue')
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/pages/login/page.vue')
        },
        {
            path: '/404',
            name: '404',
            component: () => import('@/pages/404.vue'),
            beforeEnter: (to, from, next) => {
                console.log('Router Guard')
                console.log(to, from)

                next()
            }
        },
        // 扫描pages下vue
        ...routes,
        // 扫描admin下vue
        {
            name: 'admin',
            path: '/admin',
            component: () => import('@/admin/base.vue'),
            children: [
                ...adminRoutes
            ]
        },
        {
            path: '/:catchAll(.*)*',
            // 上面的都匹配不到，就重定向到404
            redirect: {name: '404'}
        }
    ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
    console.log('Global Guard')
    console.log(to, from)
    // 如果路由元信息中有 title，则设置 document.title
    if (to.meta && to.meta.title) {
        document.title = to.meta.title
    }
    next()
})

export default router